package com.dudu.timer;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.BeanUtils;

import com.dudu.access.IMSDialUtils;
import com.dudu.bean.ImsChargeBean;
import com.dudu.dao.ImsChargeDao;
import com.dudu.dao.PoolAbilityInfoDao;
import com.dudu.model.ImsCharge;
import com.dudu.model.PoolAbilityInfo;
import com.dudu.utils.DateUtils;

import core.CoreDao;
import core.CoreDaoInvoke;
/**
 * IMS号码点数定时轮循--套餐定时监测
 * @author 危东
 * @date 2016年5月25日
 *
 */
public class OrderJob implements Job {
	
	private static final Logger log =  core.util.Logger.getLogger(OrderJob.class.getSimpleName());
	//数据库连接
	private CoreDao dao = CoreDaoInvoke.getInstance();
	//IMS号码点数记录实体类
	ImsChargeDao imsChargeDao = new ImsChargeDao();
	PoolAbilityInfoDao poolAbilityInfoDao = new PoolAbilityInfoDao();
	
	
	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {

		log.debug("开始定时执行检查套餐余额任务任务");
		//查询所有的号码
		List<PoolAbilityInfo> numbers = poolAbilityInfoDao.queryAllPoolAbilityNumber(dao);
		//判断查询出的号码是否为空
		if(numbers == null || numbers.size()==0){
			numbers = new ArrayList<PoolAbilityInfo>();
		}
		 
		 if(numbers != null && numbers.size()>0){
			 
			 for (int i=0;i<numbers.size();i++) {
				 //获取相应账号和密码
				 PoolAbilityInfo poolNumber = numbers.get(i);
				 String chargeNbr = poolNumber.getCharge_number();
				 String chargeKey = poolNumber.getCharge_key();
				 //IMS查询余额
				 String ret = IMSDialUtils.postACCOUNT(chargeNbr, chargeKey);
				 
				 //判断返回值
				 if (StringUtils.isNotBlank(ret)) {
					//获取最终返回结果
					 String result = null;
					 try {
						Document doc = DocumentHelper.parseText(ret);
						Element root = doc.getRootElement();
						Element head = root.element("Head");
						result = head.elementTextTrim("Result");
					 } catch (DocumentException e) {
						e.printStackTrace();
					 }
					 //判断查询成功
					 if(!result.equals("-1")){
						 ImsChargeBean charge = IMSDialUtils.parseAccountAndOrder(ret, chargeNbr, chargeKey);
						 
						 if (StringUtils.isNotBlank(charge.getResult()) && charge.getResult().equals("0")) {
							 
							 if (StringUtils.isNotBlank(charge.getPoints()) && StringUtils.isNotBlank(charge.getEffect_time())) {
								 int num_AllPoint = Integer.parseInt(charge.getPoints());
								 Date effectDate = DateUtils.getTimeByFormat(charge.getEffect_time(),"");
								 Date now = new Date();
								 // 时间到或者点数不足调用套餐
								 if (now.getTime() >= effectDate.getTime() || num_AllPoint < 5000) {
									 ImsCharge imsCharge = new ImsCharge();
									 BeanUtils.copyProperties(charge, imsCharge);//源,目的
									 // 插入或者更新
									 ImsCharge dbCharge = imsChargeDao.query(dao, imsCharge);
									 if (dbCharge == null) {
										 imsChargeDao.insert(dao, imsCharge);
									 } else {
										 imsChargeDao.update(dao, imsCharge);
									 }
									 
									 // 重新定购
									 IMSDialUtils.order(chargeNbr, chargeKey);
									 
								 } else {
									 ImsCharge imsCharge = new ImsCharge();
									 BeanUtils.copyProperties(charge, imsCharge);//源,目的
									 // 插入或者更新余额
									 ImsCharge dbCharge = imsChargeDao.query(dao, imsCharge);
									 if (dbCharge == null) {
										 int rtInsert = imsChargeDao.insert(dao, imsCharge);
										 
										 if (rtInsert > 0) {
											 log.debug("没到期，插入记费帐号成功");
										 } else {
											 log.debug("没到期，插入记费帐号失败");
										 }
										 
									 } else {
										 int retUpdate = imsChargeDao.update(dao, imsCharge);
										 if (retUpdate > 0) {
											 
											 log.debug("没到期，更新记费帐号成功");
										 } else {
											 log.debug("没到期，更新记费帐号成功");
											 
										 }
									 }
								 }
							 }else if(StringUtils.isBlank(charge.getEffect_time())){ //新号码初次订购
								 ImsCharge imsCharge = new ImsCharge();
								 BeanUtils.copyProperties(charge, imsCharge);//源,目的
								 // 插入或者更新
								 ImsCharge dbCharge = imsChargeDao.query(dao, imsCharge);
								 if (dbCharge == null) {
									 imsChargeDao.insert(dao, imsCharge);
								 } else {
									 imsChargeDao.update(dao, imsCharge);
								 }								 
								 // 重新定购
								 IMSDialUtils.order(chargeNbr, chargeKey);
							 }
							 
						 } else {
							 log.debug("查询套餐余额失败");
						 }
					 }else{
						 log.debug("没有找到号码：chargeNbr="+chargeNbr+";chargeKey="+chargeKey); 
					 }
				 }
				 
			 }
		 }
	}

}
